package com.edu.controller;

import com.edu.Result;
import com.edu.StatusCode;
import com.edu.pojo.EduTeacher;
import com.edu.service.EduTeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 学生管理控制层
 *
 * @author YJF
 */
@RestController
@RequestMapping("/tec")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有教师信息
     */
    @GetMapping("/all")
    public Result findAll() {
        List<EduTeacher> teacherList = eduTeacherService.findAll();
        return new Result(true, StatusCode.OK, "查询成功!", teacherList);
    }

    /**
     * 分页条件查询
     */
    @GetMapping("/list")
    public Result findByList(HttpServletRequest request) {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String tecPerId = request.getParameter("tecPerId");
        String tecName = request.getParameter("tecName");
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setTecPerId(tecPerId);
        eduTeacher.setTecName(tecName);
        PageInfo<EduTeacher> teacherList = eduTeacherService.findTec(page, size, eduTeacher);
        return new Result(true, StatusCode.OK, "查询成功!", teacherList);
    }

    /**
     * 添加教师信息
     */
    @PostMapping("/add")
    public Result addTec(@RequestBody EduTeacher eduTeacher) {
        int status = eduTeacherService.addTec(eduTeacher);
        if (status == StatusCode.REP_ERROR) {
            return new Result(false, StatusCode.REP_ERROR, "添加失败，用户已存在!", eduTeacher);
        } else if (status == StatusCode.OK) {
            return new Result(true, StatusCode.OK, "添加成功!", eduTeacher);
        }
        return new Result(false, StatusCode.ERROR, "添加失败", eduTeacher);
    }

    /**
     * 根据教师ID查询教师个人信息
     */
    @GetMapping("/one/{tecId}")
    public Result findTecById(@PathVariable("tecId") Long tecId) {
        EduTeacher eduTeacher = eduTeacherService.findTecById(tecId);
        return new Result(true, StatusCode.OK, "查询成功！", eduTeacher);
    }

    /**
     * 修改教师信息
     */
    @PutMapping("/update")
    public Result updateTec(@RequestBody EduTeacher eduTeacher) {
        int status = eduTeacherService.updateTec(eduTeacher);
        if (status == StatusCode.OK) {
            return new Result(true, StatusCode.OK, "更新成功!", eduTeacher);
        } else {
            return new Result(false, StatusCode.ERROR, "更新失败!", eduTeacher);
        }
    }

    /**
     * 根据教师ID删除教师信息
     */
    @DeleteMapping("/del/{tecId}")
    public Result delTecById(@PathVariable("tecId") Long tecId) {
        int status = eduTeacherService.delById(tecId);
        if (status == StatusCode.OK) {
            return new Result(true, StatusCode.OK, "删除成功!", tecId);
        }
        return new Result(false, StatusCode.ERROR, "删除失败!", tecId);
    }
}
